﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.ComponentModel;
using System.Collections.ObjectModel;
using ThreeByte.ActiveDeck.AddIn.Content.Controls;
using log4net;
using ThreeByte.Controls;
using ThreeByte.ActiveDeck.AddIn.Config;

namespace ThreeByte.ActiveDeck.Config
{
    public class NavigatorConfig : PageConfig
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(NavigatorConfig));

        #region Public Properties

        private ObservableCollection<DesignerControl> _elements = new ObservableCollection<DesignerControl>();
        public ObservableCollection<DesignerControl> Elements {
            get {
                return _elements;
            }
        }

        private ObservableCollection<LayerConfig> _layers = new ObservableCollection<LayerConfig>();
        public ObservableCollection<LayerConfig> Layers {
            get {
                return _layers;
            }
        }

        //private GraphicResource _backgroundImage;
        //public GraphicResource Image {
        //    get {
        //        return _backgroundImage;
        //    }
        //    set {
        //        _backgroundImage = value;
        //        NotifyPropertyChanged("Image");
        //    }
        //}

        private int _navigatorWidth = 1024;
        public int NavigatorWidth {
            get {
                return _navigatorWidth;
            }
            set {
                _navigatorWidth = value;
                NotifyPropertyChanged("NavigatorWidth");
            }
        }

        private int _navigatorHeight = 768;
        public int NavigatorHeight {
            get {
                return _navigatorHeight;
            }
            set {
                _navigatorHeight = value;
                NotifyPropertyChanged("NavigatorHeight");
            }
        }

        private int _offsetX;
        public int OffsetX {
            get {
                return _offsetX;
            }
            set {
                _offsetX = value;
                NotifyPropertyChanged("OffsetX");
            }
        }

        private int _offsetY;
        public int OffsetY {
            get {
                return _offsetY;
            }
            set {
                _offsetY = value;
                NotifyPropertyChanged("OffsetY");
            }
        }

        private bool _zoomEnabled;
        public bool ZoomEnabled {
            get {
                return _zoomEnabled;
            }
            set {
                _zoomEnabled = value;
                NotifyPropertyChanged("ZoomEnabled");
            }
        }

        #endregion Public Properties


        public NavigatorConfig() {

            this.PropertyChanged += new PropertyChangedEventHandler(NavigatorConfig_PropertyChanged);
        }

        void NavigatorConfig_PropertyChanged(object sender, PropertyChangedEventArgs e) {
            if(e.PropertyName == "Background") {
                if(Background != null) {
                    NavigatorWidth = Background.Image.PixelWidth;
                    NavigatorHeight = Background.Image.PixelHeight;
                } else {
                    NavigatorWidth = (int)(DesignerCanvas.DefaultScreen.MaxX);
                    NavigatorHeight = (int)(DesignerCanvas.DefaultScreen.MaxY);
                }
            }
        }

        public NavigatorConfig(XElement node, Dictionary<Guid, GraphicResource> resources)
            : base(node, resources) {

            NavigatorWidth = int.Parse(node.Attribute("NavigatorWidth").Value);
            NavigatorHeight = int.Parse(node.Attribute("NavigatorHeight").Value);

            if(node.Attribute("OffsetX") != null) {
                OffsetX = int.Parse(node.Attribute("OffsetX").Value);
            }
            if(node.Attribute("OffsetY") != null) {
                OffsetY = int.Parse(node.Attribute("OffsetY").Value);
            }
            if(node.Attribute("ZoomEnabled") != null) {
                ZoomEnabled = bool.Parse(node.Attribute("ZoomEnabled").Value);
            }

            foreach(XElement n in node.Elements(ns + "DesignerControl")) {
                try {
                    DesignerControl newControl = DesignerControl.FromXml(n, resources);
                    Elements.Add(newControl);
                } catch(Exception ex) {
                    log.Error("Could not create a designer control...continuing", ex);
                }
            }

            foreach(XElement layerNode in node.Elements(LayerConfig.XML_NAME)){
                try {
                    Layers.Add(LayerConfig.FromXml(layerNode, resources));
                } catch(Exception ex) {
                    log.Warn("Cannot reconstruct Layer", ex);
                }
            }
            this.PropertyChanged += new PropertyChangedEventHandler(NavigatorConfig_PropertyChanged);
        }

        public override XElement ToXml() {
            XElement node = base.ToXml();

            node.Add(new XAttribute("NavigatorWidth", NavigatorWidth));
            node.Add(new XAttribute("NavigatorHeight", NavigatorHeight));
            node.Add(new XAttribute("OffsetX", OffsetX));
            node.Add(new XAttribute("OffsetY", OffsetY));
            node.Add(new XAttribute("ZoomEnabled", ZoomEnabled));

            foreach(DesignerControl d in Elements) {
                node.Add(d.ToXml());
            }

            foreach(LayerConfig layer in Layers){
                node.Add(layer.ToXml());
            }
            return node;
        }

        public override List<GraphicResource> GetPersistentGraphicResources() {
            List<GraphicResource> imageList = new List<GraphicResource>();
            foreach(DesignerControl d in Elements) {
                foreach(GraphicResource g in d.GetGraphicResources()) {
                    imageList.Add(g);
                }
            }
            //foreach(LayerConfig layer in Layers) {
            //    foreach(DesignerControl d in layer.Elements) {
            //        foreach(GraphicResource g in d.GetGraphicResources()) {
            //            imageList.Add(g);
            //        }
            //    }
            //}

            return imageList;
        }
 
    }
}
